package com.trolls.client;


import gwtquery.plugins.draggable.client.DraggableOptions.RevertOption;
import gwtquery.plugins.draggable.client.gwt.DraggableWidget;
import gwtquery.plugins.droppable.client.events.DropEvent;
import gwtquery.plugins.droppable.client.events.DropEvent.DropEventHandler;
import gwtquery.plugins.droppable.client.gwt.DroppableWidget;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Cursor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;


public class CompositeTabList extends Gallery{
	private VerticalPanel vPanelList = new VerticalPanel();
	// Déclaration des panels et boutons
	private ArrayList<Item> items;
	private int NB_COLONNES = 3;
	private int NB_LIGNES_MAX = 3;
	private PushButton buttonPrecedent = new PushButton(new Image(GWT.getHostPageBaseURL()+"images/util/previousTop.png"));
	private PushButton buttonSuivant = new PushButton(new Image(GWT.getHostPageBaseURL()+"images/util/nextBottom.png"));
	private Grid gridItems;
	public CompositeTabList(ArrayList<Item> listItems, int nb_colonnes,int nb_lignes_max) {
		this(listItems);
		NB_COLONNES = nb_colonnes;
		NB_LIGNES_MAX = nb_lignes_max;

	}
	public CompositeTabList(ArrayList<Item> listItems) {
		super(9, listItems.size());
		position = NB_ITEM_MAX - 1;
		items = listItems;
		VerticalPanel vPanelPrincipal = new VerticalPanel();
		vPanelPrincipal.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
		HTML titreListe = new HTML("Liste des Trolls");
		titreListe.setStyleName("titre3 titreListe");
		vPanelPrincipal.add(titreListe);
		vPanelList = new VerticalPanel();
		vPanelList.setStyleName("listTxt");
		// create a DragController to manage drag-n-drop actions
	    // note: This creates an implicit DropController for the boundary panel
		HorizontalPanel hpanelGrid = new HorizontalPanel();
		VerticalPanel vpanelButtons = new VerticalPanel();
		gridItems = new Grid(NB_COLONNES, NB_LIGNES_MAX);
		 // Put some values in the grid cells.
		 	int numItem=0;
		    for (int row = 0; row < NB_LIGNES_MAX; ++row) {
				for (int col = 0; col < NB_COLONNES && numItem<items.size(); ++col){
					System.out.println(numItem);
					final Item item = items.get(numItem);
					gridItems.setWidget(row, col, createDraggableImage(item.getImagePath(),item));
					numItem++;
			    }
			}
		//GESTION NEXT PREV
		 if(NB_ITEM_MAX<listItems.size()){
		    // Evenement du clic sur 'Précédent'
		buttonPrecedent.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				gridItems.clear();
				InitPrecedent();
				int nbItems=0;
				
				for (int row = 0; row < NB_LIGNES_MAX; ++row) {
					for (int col = 0; col < NB_COLONNES && nbItems<NB_ITEM_MAX; ++col){
						int numItem =getSuivant();
				      	final Item item = items.get(numItem);
						gridItems.setWidget(row, col, createDraggableImage(item.getImagePath(),item));
						nbItems++;
						
				    }
				}

			}
		});
		// Evenement du clic sur 'Suivant'
		buttonSuivant.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				gridItems.clear();
				InitSuivant();
				int nbItems=0;
				for (int row = 0; row < NB_LIGNES_MAX; ++row) {
					for (int col = 0; col < NB_COLONNES && nbItems<NB_ITEM_MAX; ++col){
						int numItem =getSuivant();
				      	final Item item = items.get(numItem);
						gridItems.setWidget(row, col, createDraggableImage(item.getImagePath(),item));
						nbItems++;
				    }
				}
			}
		});
		vpanelButtons.add(buttonPrecedent);
		vpanelButtons.add(buttonSuivant);
		 }
		hpanelGrid.add(vpanelButtons);
		hpanelGrid.add(gridItems);
		vPanelPrincipal.add(hpanelGrid);
		vPanelPrincipal.add(createTrashBin());
		this.initWidget(vPanelPrincipal);
	}
	private Image configImg(final Image img,final Item item) {
		img.addStyleName("pc-template-img");
		img.setWidth("70px");
		img.setHeight("100px");
		img.setTitle(item.getName());
		img.setAltText(item.getName());
		img.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				item.afficherItem(LaTerreDesTrolls.get().getContent());
				setPositionSelected(position);
				//img.addStyleName("itemSelected");
			}
		});
		if(position == getPositionSelected()){
			System.out.println("Position "+position);
			img.addStyleName("itemSelected");
		}
		return img;

	}
	/**
	 * Creation des images déplacables
	 * @param path
	 * @param item
	 * @return
	 */
	  private Widget createDraggableImage(String path, Item item) {
		    Image img = new Image(path);
		    img = configImg(img, item);
		    // make the imgae draggable
		    DraggableWidget<Image> draggableImg = new DraggableWidget<Image>(img);
		    draggableImg.setStylePrimaryName((String.valueOf(item.getId())));
		    // revert the image to its initial position if no drop
		    draggableImg.setRevert(RevertOption.ON_INVALID_DROP);
		    // ensure that dragging image is above the others during drag operation
		    draggableImg.setDraggingZIndex(100);
		    // set the cursor during the drag operation
		    draggableImg.setDraggingCursor(Cursor.MOVE);
		    return draggableImg;
		  }

	  /**
	   * creation de la poubelle(drop) de suppression 
	   *
	   * @return
	   */
	  private Widget createTrashBin() {

	    Image trash = new Image(GWT.getHostPageBaseURL()+"images/corbeille.png");

	    // make the label droppable
	    DroppableWidget<Image> trashBin = new DroppableWidget<Image>(trash);
	    // add a drop handler
	    trashBin.addDropHandler(new DropEventHandler() {

	      public void onDrop(DropEvent event) {
	        Widget droppedImage = event.getDraggableWidget();
	        int id = Integer.parseInt(droppedImage.getStylePrimaryName());
	        Troll troll = new Troll(id) ;
	        troll.delete();
	        droppedImage.removeFromParent();
	      }
	    });

	    // return the droppable widget
	    return trashBin;
	  }


}
